//
//  orderDefeat.m
//  BeautyPicture
//
//  Created by 沈振邦 on 15/12/7.
//  Copyright © 2015年 szb. All rights reserved.
//

#import "orderDefeat.h"
#import "headPromptView.h"
#import "BPhttpTool.h"
#import "MJExtension.h"
#import "MBProgressHUD+MJ.h"
#import "AppDelegate.h"
#import "BPNavButtonView.h"

@implementation orderDefeat

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUpView];
        UIView *naView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, BPScreenW, BPScreenNavY)];
        naView.backgroundColor = BPGlobaThemeColor;
        [self addSubview:naView];
        
        BPNavButtonView *button = [[BPNavButtonView alloc] init];
        [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
        [button setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        [button setTitle:@"返回" forState:UIControlStateNormal];
        button.frame = CGRectMake(15, 16.0, 80, BPScreenNavY - 16.0);
        [naView addSubview:button];
        [button addTarget:self action:@selector(dismissView) forControlEvents:UIControlEventTouchUpInside];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 16.0, 100, BPScreenNavY - 16.0)];
        titleLabel.center = CGPointMake(BPScreenW / 2.0, titleLabel.height / 2.0 + 16.0);
        titleLabel.text = @"提交";
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = [UIFont boldSystemFontOfSize:20.0f];
        [naView addSubview:titleLabel];
    }
    return self;
}

- (void)dismissView{
    [D_Main_Appdelegate hideDefeatView];
}

- (void)setUpView {

    self.backgroundColor = [UIColor whiteColor];
    
    //设置头部提示view
    headPromptView *headView = [[headPromptView alloc] initWithFrame:CGRectMake(0, BPScreenNavY + 30.0, BPScreenW, 100)];
    [headView setUpPromptView:@"defeatOrder" and:@"亲,您的下单失败了" withDetailInfo:nil];
    [self addSubview:headView];
    
    CGFloat labelY = CGRectGetMaxY(headView.frame) +10;
    UILabel * promptLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, labelY, BPScreenW - 60, 40)];
    promptLabel.font = [UIFont systemFontOfSize:18.0f];
    [self addSubview:promptLabel];
    promptLabel.textAlignment = NSTextAlignmentCenter;
    promptLabel.text = @"您的网络不给力,请重新下单";
    
    UIButton *editingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    CGFloat btnY2 = CGRectGetMaxY(promptLabel.frame) + 50;
    CGFloat btnH = 33.0f;
    CGFloat btnW = 110;
    editingBtn.size = CGSizeMake(btnW, btnH);
    editingBtn.center = CGPointMake(BPScreenW / 2.0, btnY2);
    [editingBtn.layer setMasksToBounds:YES];
    [editingBtn.layer setCornerRadius:15.0f];
    editingBtn.backgroundColor = BPGlobaThemeColor;
    [editingBtn setTitle:@"重新下单" forState:UIControlStateNormal];
    [editingBtn setTintColor:[UIColor whiteColor]];
    [self addSubview:editingBtn];
    [editingBtn addTarget:self action:@selector(continueEdit) forControlEvents:UIControlEventTouchUpInside];
}

- (void)continueEdit
{
    NSDictionary *param = [NSDictionary dictionary];
    param = [self.orderData keyValues];
    [BPhttpTool post:[NSString stringWithFormat:@"%@order/commit",BPhttp] params:param success:^(id responseObj) {
        NSDictionary *dic = responseObj;
        if ([dic[@"status"] intValue] == 1) {
            [D_Main_Appdelegate showPayView];
//            [[D_Main_Appdelegate payView] setUpHeadView:dic[@"info"]];
            [D_Main_Appdelegate payView].price = self.orderData.price;
            
        }else{
            [MBProgressHUD showError:@"下单失败"];
        }
        
    } failure:^(NSError *error) {
        [MBProgressHUD showError:@"下单失败"];
    }];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
